<template>
    <div id="box">
        <div id="search" style="padding-left: 150px">
            <el-form :inline="true" :model="searchForm" class="demo-form-inline">
                <el-form-item label="学生姓名">
                    <el-input v-model="searchForm.studentName" placeholder="学生姓名"></el-input>
                </el-form-item>
                <el-form-item label="科目">
                    <el-input v-model="searchForm.examContent" placeholder="考试科目"></el-input>
                </el-form-item>
                <el-form-item label="班级">
                    <el-input v-model="searchForm.clazz" placeholder="班级"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="searchSubmit">查询</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div id="headerTool" style="padding-left: 150px;margin-bottom: 20px">
            <el-button type="primary" plain @click="addInfo" style="margin-right: 5px">新增</el-button>
            <el-popconfirm
                title="确定删除选中行数据？"
                @confirm="confirm"
                @cancel="cancel"
            >
                <el-button type="danger" slot="reference" plain>批量删除</el-button>
            </el-popconfirm>
        </div>
        <el-table
                :data="tableData"
                @selection-change="handleSelectionChange"
                style="width: 80%;margin: 0 auto">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>

            <el-table-column
                    prop="id"
                    label="ID">
            </el-table-column>
            <el-table-column
                    prop="studentNo"
                    label="学号">
            </el-table-column>
            <el-table-column
                    prop="studentName"
                    label="姓名">
            </el-table-column>

            <el-table-column
                    prop="clazz"
                    label="班级">
            </el-table-column>

            <el-table-column
                    prop="examContent"
                    label="科目">
            </el-table-column>
            <el-table-column
                    prop="score"
                    label="分数">
            </el-table-column>
            <el-table-column
                    prop="examTime"
                    label="考试时间">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.row)">编辑
                    </el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>

        </el-table>
        <div class="block" style="padding-left: 150px;margin-top: 20px">
            <!--      current-page: 当前页   page-size: 每页展示的条数    total 数据总条数  page-sizes 下拉选选项 -->
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="1"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="5"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearInfo">
            <el-form :model="scoreForm" ref="scoreForm" :label-position="labelPosition" label-width="80px"
                     :rules="rules">
                <el-form-item v-show="false">
                    <el-input v-model="scoreForm.id" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="学号" prop="studentNo">
                    <el-input v-model="scoreForm.studentNo" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="姓名" prop="studentName">
                    <el-input v-model="scoreForm.studentName" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="班级" prop="clazz">
                    <el-input v-model="scoreForm.clazz" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="科目" prop="examContent">
                    <el-input v-model="scoreForm.examContent" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="分数" prop="score">
                    <el-input v-model="scoreForm.score" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="考试时间" prop="examTime">
                    <el-date-picker type="date" placeholder="选择日期" v-model="scoreForm.examTime"
                                    style="width: 100%;"></el-date-picker>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="onSubmit">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import instance from "@/http";

export default {
    name: "ScoreView",
    data() {
        return {
            total:10,
            title: '新增',
            rules: {
                studentName: [
                    {required: true, message: '请输入学生姓名', trigger: 'blur'},
                ],
                clazz: [
                    {required: true, message: '请输入班级', trigger: 'blur'},
                ],
                examTime: [
                    {required: true, message: '请输入考试时间', trigger: 'blur'},
                ],
                examContent: [
                    {required: true, message: '请输入考试科目', trigger: 'blur'},
                ],
                score: [
                    {required: true, message: '请输入分数', trigger: 'blur'},
                ],
                studentNo: [
                    {required: true, message: '请输入学号', trigger: 'blur'},
                ]
            },
            labelPosition: 'right',
            editDialogFormVisible: false,
            dialogFormVisible: false,
            scoreForm: {
                id: '',
                studentNo: '',
                studentName: '',
                clazz: '',
                examContent: '',
                examTime: '',
                score: ''
            },
            searchForm: {
                studentName: '',
                clazz: '',
                examContent: '',
                page: 1,
                limit: 5
            },
            tableData: [
                {
                    id: '1',
                    studentNo: '20250108001',
                    studentName: 'zhangsan',
                    clazz: '实训1班',
                    examContent: 'Springboot',
                    examTime: '2025-01-07',
                    score: '98'
                }
            ],
            ids:[]
        }
    },
    methods: {
        confirm(){
            // 判定用户是否已经选择的数据
            if (this.ids.length==0){
                this.$notify.warning("请至少选择一条数据")
                return false
            }

            instance.post("/score/batchDelete",this.ids)
                .then(res=>{
                    if (res.data.code==0){
                        this.$notify.success(res.data.message)
                    }else {
                        this.$notify.error(res.data.message)
                    }
                    this.scorePage()
                })
        },
        cancel(){
            this.$notify.info('删除已取消')
        },
        // 关闭弹窗时 重置数据
        clearInfo() {
            this.$refs.scoreForm.clearValidate()
            this.title = "新增"
            this.scoreForm={
                id: '',
                studentNo: '',
                studentName: '',
                clazz: '',
                examContent: '',
                examTime: '',
                score: ''
            }
        },
        handleEdit(row) {
            this.title = '编辑'
            this.dialogFormVisible = true
            // 将当前行数据回显在表单中
            this.scoreForm = row
            // 发送http请求
        },
        handleDelete(row) {
            // 确认框 避免用户的误操作
            this.$confirm('确认删除当前行数据?', '删除', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 发送http请求
                instance.post("/score/batchDelete",[row.id])
                    .then(res=>{
                        if (res.data.code==0){
                            this.$notify.success(res.data.message)
                        }else {
                            this.$notify.error(res.data.message)
                        }
                        this.scorePage()
                    })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        handleSelectionChange(val) {
            // val 是选中行的所有数据  一般在批量删除时 只需要当前行的id即可   所以可以遍历所有选中行数据  筛选id  箭头函数
            this.ids = val.map(row => row.id)  // []
        },
        //  改变每页显示数据条数时 会自动调用该函数  参数:当前页允许显示数据的条数
        handleSizeChange(limit) {
            this.searchForm.limit=limit
            this.scorePage()
        },
        // 改变页码时会自动调用该函数    参数 :当前页的页码
        handleCurrentChange(currentPage) {
            this.searchForm.page = currentPage
            this.scorePage()
        },
        onSubmit() {
            //  如何实现区分数据时添加数据   => 添加请求    还是修改数据 => 修改请求
            instance.post("/score/addOrUpdate",this.scoreForm)
                .then(res=>{
                    if (res.data.code==0){
                        this.$notify.success(res.data.message)
                        this.dialogFormVisible =false
                    }else {
                        this.$notify.error(res.data.message)
                    }
                    this.scorePage()
                })
        },
        searchSubmit() {
            this.searchForm.page=1
            this.scorePage()
        },
        addInfo() {
            // 显示弹出层
            this.dialogFormVisible = true
        },
        scorePage(){
            instance.post('/score/page',this.searchForm)
                .then(res=>{
                    if (res.data.code == 0) {
                        this.total = res.data.data.total
                        this.tableData = res.data.data.tableData
                    }
                })
        }
    },
    created() {
        this.scorePage()
    }
}
</script>

<style scoped>

</style>